//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    zzry:'',
    jjsy:'',
    dbTOP:'',
    url:'',
    yello: '',
    gray:'',
    boo: true,
    search:''
  },
  getname(e){
    if (e.detail.value != 0) {
      this.setData({
        boo: false
      })
    }else{
      this.setData({
        boo: true
      })
    }
    let that = this;
    wx.request({
      url: 'http://t.yushu.im/v2/movie/search?q='+e.detail.value,
      data: '',
      header: {},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        that.star(res.data.subjects)
        that.setData({
          search: res.data.subjects
        })
        wx.setStorage({
          key: 'search',
          data: res.data.subjects
        })
      }
    })
  },
  onShow(){
    wx.setNavigationBarTitle({
      title: '光与影'
    })
  },
  onLoad(){
    this.ajax({ url: 'http://t.yushu.im/v2/movie/in_theaters', name:'zzry'})
    this.ajax({ url: 'http://t.yushu.im/v2/movie/coming_soon', name:'jjsy'})
    this.ajax({ url: 'http://t.yushu.im/v2/movie/top250', name:'dbTOP'})
  },
  navto(e){
    let idx = e.currentTarget.id;
    this.setData({
      index: idx
    })
    wx.navigateTo({
      url: '/pages/dy_details/dy_details?index=' + this.data.index + '&icon=zzry'
    })
  },
  navtoo(e){
    let idx = e.currentTarget.id;
    this.setData({
      index: idx
    })
    wx.navigateTo({
      url: '/pages/dy_details/dy_details?index=' + this.data.index + '&icon=jjsy'
    })
  },
  navtooo(e){
    let idx = e.currentTarget.id;
    this.setData({
      index: idx
    })
    wx.navigateTo({
      url: '/pages/dy_details/dy_details?index=' + this.data.index + '&icon=dbTOP'
    })
  },
  navt(e){
    let idx = e.currentTarget.id;
    console.log(e)
    this.setData({
      index: idx
    })
    wx.navigateTo({
      url: '/pages/dy_details/dy_details?index=' + this.data.index + '&icon=search'
    })
  },
  star(res){
    let yello,gray;
    for(let i = 0 ; i < res.length; i++) {
      if (res[i].rating.average >= 9) {
        res[i].rating.yello = 5;
        res[i].rating.gray = 0;
      }else if (res[i].rating.average >= 7) {
        res[i].rating.yello = 4;
        res[i].rating.gray = 1;
      }else if (res[i].rating.average >= 5) {
        res[i].rating.yello = 3;
        res[i].rating.gray = 2;
      }else if (res[i].rating.average >= 3) {
        res[i].rating.yello = 2;
        res[i].rating.gray = 3;
      }else if (res[i].rating.average >= 1) {
        res[i].rating.yello = 1;
        res[i].rating.gray = 4;
      }else{
        res[i].rating.yello = 0;
        res[i].rating.gray = 5;
      }
    }
    return res
  },
  ajax(obj) {
    let that = this;
    wx.request({
      url: obj.url,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        let key = obj.name
        that.setData({
          [key]: that.star(res.data.subjects)
        })
        wx.setStorage({
          key: key,
          data: res.data.subjects
        })
      }
    })
  }
})
